package com.njtoyo.taxi.admin.service.business.customer.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njtoyo.taxi.admin.mapper.master.entity.CustomerTag;
import com.njtoyo.taxi.admin.mapper.master.mapper.CustomerTagMapper;
import com.njtoyo.taxi.admin.service.business.customer.CustomerTagService;
import com.njtoyo.taxi.entity.backend.AdminUser;
import com.taxi.entity.common.RestResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class CustomerTagServiceImpl extends ServiceImpl<CustomerTagMapper, CustomerTag> implements CustomerTagService {


    @Override
    public RestResult selectTag(AdminUser adminUser, Dict dict) {
        Long simpleCustomerId = dict.getLong("simpleCustomerId");
        Long customerId = dict.getLong("customerId");
        String tag = dict.getStr("tag");

        baseMapper.delete(Wrappers.<CustomerTag>lambdaQuery().eq(CustomerTag::getSimpleCustomerId, customerId));
        if (StrUtil.isNotBlank(tag) && (ObjectUtil.isNotNull(customerId) || ObjectUtil.isNotNull(simpleCustomerId))) {
            CustomerTag customerTag = new CustomerTag();
            String [] tags = tag.split(",");
            if (ObjectUtil.isNotNull(customerId)) {
                customerTag.setCustomerId(customerId);
                baseMapper.delete(Wrappers.<CustomerTag>lambdaQuery().eq(CustomerTag::getCustomerId, customerId));
            } else {
                baseMapper.delete(Wrappers.<CustomerTag>lambdaQuery().eq(CustomerTag::getSimpleCustomerId, simpleCustomerId));
                customerTag.setSimpleCustomerId(simpleCustomerId);
            }
            customerTag.setTag(0);
            baseMapper.insert(customerTag);
        } else {
            if (ObjectUtil.isNotNull(customerId)) {
                baseMapper.delete(Wrappers.<CustomerTag>lambdaQuery().eq(CustomerTag::getCustomerId, customerId));
            } else {
                baseMapper.delete(Wrappers.<CustomerTag>lambdaQuery().eq(CustomerTag::getSimpleCustomerId, simpleCustomerId));
            }
        }
        return RestResult.success();
    }
}
